(function() {
  'use strict';

  angular
    .module('hp')
    .controller('RegisterController', RegisterController);

  /** @ngInject */
  function RegisterController($log,$timeout, webDevTec, toastr,$state,$scope,RegisterService,MyAuth,ProvinceService,CityService,$rootScope) {
    var vm = this;
    vm.invalid=false
    vm.msg=''
    vm.form = {
        education:'本科',
        sex:'1'
    };
    vm.rules = {
        phone:/^1[1|2|3|4|5|6|7|8|9]\d{9}$/,
        cname:/[\u4e00-\u9fa0]+/,
        email:/^[\w.-]+@\w+(\.\w+)?\.\w{2,3}/gi
    }
    vm.submit = submit;
    vm.getCity = getCity;
    vm.provinces = [];
    vm.cities = [];
    vm.selectedProvince = {provinceId:0,province:'请选择'};
    vm.selectedCity = {cityId:0,city:'请选择'};
    vm.cityChange = cityChange;
    // vm.city = '';

    ProvinceService.get({}).$promise.then(function(data){
        var c = data || {};
        var code = c.head.errorCode + '';
        switch (code) {
            case '200':
                vm.provinces = c.body?c.body.list?c.body.list:[]:[];
                // if(vm.provinces.length>0){
                //     vm.selectedProvince = vm.provinces[0];
                // }
                break;
            default:
                toastr.info('' + c.head.errorMsg)
        }
    },function(err){
        toastr.info(''+err)
        $log.debug(err);
    })

    function getCity(p){
        // console.log(p)
        if(!p){
            vm.cities = [];
            return;
        }
        vm.form.province = p.province;
        CityService.get({body:{proId:p.provinceId}}).$promise.then(function(data){
            var c = data || {};
            var code = c.head.errorCode + '';
            if (code == '200'){
                    vm.cities = c.body?c.body.list?c.body.list:[]:[];
            }else{
                    toastr.info('' + c.head.errorMsg)
            }
        },function(err){
            toastr.info('服务器正忙!'+err)
            $log.debug(err);
        })
    }

    function cityChange(c){
        // console.log(c);
        if(!c) return;
        vm.form.city = c.city;
    }

    function checkdata(){
        if(!/^[\w.-]+@\w+(\.\w+)?\.\w{2,3}/gi.test(vm.form.email)){
            vm.invalid = true;
            vm.msg = '邮箱格式不正确'
        }
        if(!/^1[1|2|3|4|5|6|7|8|9]\d{9}$/.test(vm.form.phone)){
            vm.invalid = true;
            vm.msg = '手机号格式不正确'
        }
        // if(!/[\u4e00-\u9fa0]+/.test(vm.form.username)){
        //     vm.invalid = true;
        // }
        // return vm.invalid;
    }

    function submit(){
        $log.debug('register')
        vm.invalid = false
        checkdata();
        if(vm.invalid){
            return;
        }
        RegisterService.post({body:vm.form}).$promise.then(function(data){
            var c = data || {};
            var code = c.head.errorCode + '';
            switch (code) {
                case '200':
                    MyAuth.setData('token',c.body.token);
                    $rootScope.isLogin = MyAuth.loggedIn();
                    toastr.info('注册成功');
                    $state.go('machinecode')
                    break;
                
                default:
                    toastr.info('' + c.head.errorMsg)
            }
        },function(err){
            toastr.info(''+err)
            $log.debug(err);
        });

    }

  }
})();
